package com.flyzing.genl.dto;

import com.flyzing.genl.po.ColumnPO;
import com.flyzing.genl.util.StringUtil;

/**
 * @author yaoguangyao
 * @Date 2020年12月30日
 */
public class ClazzProperty {
	private String columnName;
	private String jdbcType;
	private String propertyType;
	private String propertyName;
	private String comment;

	private String setMethodName;
	private String getMethodName;
	private String simplePropertyType;
	private boolean isPrimaryKey;

	public String getPropertyType() {
		return propertyType;
	}

	public void setPropertyType(String propertyType) {
		this.propertyType = propertyType;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getJdbcType() {
		return jdbcType;
	}

	public void setJdbcType(String jdbcType) {
		this.jdbcType = jdbcType;
	}
	
	public String getSetMethodName() {
		return setMethodName;
	}

	public void setSetMethodName(String setMethodName) {
		this.setMethodName = setMethodName;
	}

	public String getGetMethodName() {
		return getMethodName;
	}

	public void setGetMethodName(String getMethodName) {
		this.getMethodName = getMethodName;
	}

	public String getSimplePropertyType() {
		return simplePropertyType;
	}

	public void setSimplePropertyType(String simplePropertyType) {
		this.simplePropertyType = simplePropertyType;
	}
	
	public boolean isPrimaryKey() {
		return isPrimaryKey;
	}

	public void setPrimaryKey(boolean isPrimaryKey) {
		this.isPrimaryKey = isPrimaryKey;
	}

	/**
	 * 列转换为属性
	 * @param column
	 * @return
	 */
	public static ClazzProperty getFromPO(ColumnPO column) {
		ClazzProperty property = new ClazzProperty();
		property.setColumnName(column.getColumnName());
		property.setJdbcType(column.getDataType().toUpperCase());
		property.setComment(column.getColumnComment());
		
		if("PRI".equals(column.getColumnKey().toUpperCase())) {
			property.setPrimaryKey(true);
		} else {
			property.setPrimaryKey(false);
		}
		property.setPropertyName(StringUtil.convertUnderline(property.getColumnName(), false));
		
		String propertyName = property.getPropertyName();
		String upperCasePropertyName = StringUtil.upperCatureName(propertyName);
		property.setGetMethodName("get" + upperCasePropertyName);
		property.setSetMethodName("set" + upperCasePropertyName);
		
		String jdbcType = property.getJdbcType();
		
		if("INT".equals(jdbcType)) {
			//mybatis不支持INT  No enum constant org.apache.ibatis.type.JdbcType.INT
			jdbcType = "INTEGER";
			property.setJdbcType(jdbcType); 
		}
		if("DATETIME".equals(jdbcType)) {
			//老版本mybatis不支持DATETIME  No enum constant org.apache.ibatis.type.JdbcType.INT
			jdbcType = "TIMESTAMP";
			property.setJdbcType(jdbcType); 
		}
		if ("BIGINT".equals(jdbcType)) {
			property.setPropertyType("java.lang.Long");
			property.setSimplePropertyType("Long");
		} else if (jdbcType.indexOf("INT") > -1) {
			property.setPropertyType("java.lang.Integer");
		} else if (jdbcType.indexOf("CHAR") > -1 || jdbcType.indexOf("TEXT") > -1) {
			property.setPropertyType("java.lang.String");
		} else if (jdbcType.indexOf("DATE") > -1 || jdbcType.indexOf("TIME") > -1) {
			property.setPropertyType("java.util.Date");
		} else if ("FLOAT".equals(jdbcType) || "DOUBLE".equals(jdbcType)) {
			property.setPropertyType("java.lang.Double");
		} else if ("DECIMAL".equals(jdbcType)) {
			property.setPropertyType("java.math.BigDecimal");
		} else if ("BLOB".equals(jdbcType)) {
			property.setPropertyType("java.lang.byte[]");
		} else if ("JSON".equals(jdbcType)) {
			property.setJdbcType("OTHER");
			property.setPropertyType("java.lang.String");
		} else {
			throw new RuntimeException("不支持此列类型jdbcType=" + jdbcType);
		}
		
		String propertyType = property.getPropertyType();
		String simplePropertyType = propertyType.substring(propertyType.lastIndexOf(".") + 1);
		property.setSimplePropertyType(simplePropertyType);
		return property;
	}

}
